package com.douqu.game.core.entity.world.map;

import com.bean.core.buffer.ByteBuffer;
import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.map.CityArmyConfig;
import com.douqu.game.core.config.map.CityArmyGroupConfig;
import com.douqu.game.core.e.E_CityInOutType;
import com.douqu.game.core.entity.battle.BattleTemp;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.protobuf.SGCommonProto;

/**
 * 大地图远征军
 * @Author: Bean
 * @Description:
 * @Date: 2018-05-23 17:11
 */
public class SpriteExpeditionBean extends SpriteBean {

    /**
     * 军队ID
     */
    public int armyId;

    /**
     * 属性组ID
     */
    public int groupId;

    /**
     * 路线列表
     */
    private int[] routeList;

    /**
     * 当前所在城市
     */
    private int curRouteIndex = -1;

    /**
     * 结束时间(到了后删除)
     */
    private long endTime;

    public SpriteExpeditionBean()
    {
        this.curRouteIndex = -1;
    }

    public SpriteExpeditionBean(int tempId, int armyId, int groupId, int cityId, int[] routeList, SGCommonProto.E_ARMY_TEAM team, SGCommonProto.E_ARMY_STATUS status, long endTime)
    {
        this();

        this.armyId = armyId;
        this.groupId = groupId;
        this.routeList = routeList;
        String uniqueId = "EX" + TimeUtils.getCurrentTime("ssSSS") + tempId;
        this.objectIndex = armyId + ConstantFactory.UNDERLINE + groupId + ConstantFactory.UNDERLINE + uniqueId + ConstantFactory.UNDERLINE + cityId;
        this.team = team;
        this.endTime = endTime;
        setCurCity(cityId);
        setStatus(status);

        updateBaseInfo();
        initBattleTemp();
    }


    @Override
    public void loadFrom(ByteBuffer buffer)
    {
        super.loadFrom(buffer);

        armyId = buffer.readInt();
        groupId = buffer.readInt();

        long endTimeInt = buffer.readInt();
        endTime = endTimeInt * 1000;

        curRouteIndex = buffer.readByte();
        int size = buffer.readByte();
        routeList = new int[size];
        for(int i = 0; i < size; i++)
        {
            routeList[i] = buffer.readInt();
        }

        baseInfo.setObjectIndex(objectIndex);
    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
        super.writeTo(buffer);

        buffer.writeInt(armyId);
        buffer.writeInt(groupId);

        int endTimeInt = (int) (endTime / 1000);
        buffer.writeInt(endTimeInt);

        buffer.writeByte(curRouteIndex);
        buffer.writeByte(routeList.length);
        for(int cityId : routeList)
        {
            buffer.writeInt(cityId);
        }
    }

    @Override
    public void update(WorldMapUpdateResult updateResult)
    {
        if(status == SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MOVE && isOverdueStatus())
        {
//            LogUtils.info("服务器自动行走开始 -> 远征军:" + objectIndex + " 当前城:" + getCurCity() + " 路线:" + Arrays.toString(routeList));
            autoMove();
        }


    }

    public boolean isEnd()
    {
        return DataFactory.currentTime >= endTime;
    }


    @Override
    public SGCommonProto.PlayerBaseInfo.Builder parseBaseInfo()
    {
        updateBaseInfo();

        return baseInfo.parseBaseInfo();
    }


    public void updateBaseInfo()
    {
        baseInfo.setObjectIndex(objectIndex);

        CityArmyConfig cityArmyConfig = DataFactory.getInstance().getGameObject(DataFactory.CITY_ARMY_KEY, armyId);
        baseInfo.setNickName(cityArmyConfig.name);
        baseInfo.setCamp(cityArmyConfig.camp);
        baseInfo.setAvatar(cityArmyConfig.master+"");

        if(camp == 0)
            camp = cityArmyConfig.camp;

        CityArmyGroupConfig cityArmyGroupConfig = DataFactory.getInstance().getGameObject(DataFactory.CITY_ARMY_GROUP_KEY, groupId);
        baseInfo.setFc(cityArmyGroupConfig.fc);
        baseInfo.setLv(cityArmyGroupConfig.getLv());
    }

    public void initBattleTemp()
    {
        if(battleTemp == null)
        {
            battleTemp = new BattleTemp(this);
        }
    }


    /**
     * 移动
     * @return 当前最新到达的城市，为0表示非法移动
     */
    public void autoMove()
    {
        if(routeList.length == 0)
            return;

        if(isEndRoute())
            return;

        int curCityId = updateMove(E_CityInOutType.OUT_AUTO_MOVE);

        if(curCityId != 0)
        {
            CityBean cityBean = WorldInfoFactory.getInstance().getWorldMapData(masterPlayerIndex).getCity(curCityId);
            cityBean.arrive(this, false, E_CityInOutType.IN_AUTO_MOVE);
        }

//        LogUtils.info("服务器自动行走完成 -> " + objectIndex + "->" + getName() + ":" + objectIndex + " 当前城:" + getCurCity() + " 路线:" + Arrays.toString(routeList));
    }

    private int updateMove(E_CityInOutType type)
    {
        leaveCurCity(type);

        curRouteIndex++;

        int curCity = routeList[curRouteIndex];
        if (isEndRoute())
        {
            stop();
            setStatus(SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_FREE);
        }
        else
        {
            setStatus(SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MOVE);
        }

        return curCity;
    }

    public void leaveCurCity(E_CityInOutType type)
    {
        WorldMapData worldMapData = WorldInfoFactory.getInstance().getWorldMapData(masterPlayerIndex);
        CityBean cityBean = worldMapData.getCurCity(this);
        if (cityBean != null) cityBean.leave(this, null, type);
        setInCity(false);
    }

    public boolean isEndRoute()
    {
        return curRouteIndex >= routeList.length - 1;
    }


    @Override
    public void stop() {
        curRouteIndex = -1;
        routeList = new int[0];
    }

    @Override
    public String toString() {
        return "{" +
                "armyId=" + armyId +
                ", groupId=" + groupId +
                "}-" + super.toString();
    }
}
